package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.ShopImage;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.MultipartFileUtil;
import club.xyes.zkh.ecommerce.dao.mapper.ShopImageMapper;
import club.xyes.zkh.ecommerce.service.ShopImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by 郭文梁 2019/4/17 0017 09:31
 * ShopImageServiceImpl
 * 商铺图片相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/17 0017
 */
@Service
@Slf4j
public class ShopImageServiceImpl extends BaseServiceImpl<ShopImage> implements ShopImageService {
    private final WebsiteConfigurationProperties websiteConfigurationProperties;
    private final ShopImageMapper mapper;

    @Autowired
    public ShopImageServiceImpl(ShopImageMapper mapper, WebsiteConfigurationProperties websiteConfigurationProperties) {
        super(mapper);
        this.mapper = mapper;
        this.websiteConfigurationProperties = websiteConfigurationProperties;
    }

    @Override
    public ShopImage newImage(Shop shop, MultipartFile imageFile) throws IOException {
        String path = websiteConfigurationProperties.getStaticFilePath() + websiteConfigurationProperties.getShopImagePath();
        String filename = MultipartFileUtil.save(imageFile, path);
        ShopImage image = ShopImage.fromShop(shop);
        image.setAbsolutely(false);
        image.setImageUrl(filename);
        return save(image);
    }

    @Override
    public List<ShopImage> findByShopId(Integer shopId) {
        ShopImage query = new ShopImage();
        query.setShopId(shopId);
        return findByQuery(query);
    }

    @Override
    public List<ShopImage> resetShopImage(Shop shop, List<MultipartFile> files) throws IOException {
        //清除图片
        int cols = deleteByShop(shop);
        log.debug("Delete: {} ShopImage(s) has been deleted for shop {}", cols, shop);
        String path = websiteConfigurationProperties.getStaticFilePath() + websiteConfigurationProperties.getShopImagePath();
        try {
            List<ShopImage> images = files.stream()
                    .map(file -> {
                        try {
                            String filename = MultipartFileUtil.save(file, path);
                            ShopImage image = new ShopImage();
                            image.setShopId(shop.getId());
                            image.setShop(shop);
                            image.setAbsolutely(false);
                            image.setImageUrl(filename);
                            return image;
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }).collect(Collectors.toList());
            return save(images);
        } catch (RuntimeException e) {
            Throwable cause = e.getCause();
            if (cause instanceof IOException) {
                throw (IOException) cause;
            } else {
                throw e;
            }
        }
    }

    @Override
    public ShopImage replace(ShopImage image, MultipartFile imageFile) throws IOException {
        String path = websiteConfigurationProperties.getStaticFilePath()
                + websiteConfigurationProperties.getShopImagePath();
        String filename = MultipartFileUtil.save(imageFile, path);
        image.setImageUrl(filename);
        return updateById(image);
    }

    @Override
    public int deleteByShopId(Integer shopId) {
        ShopImage query = new ShopImage();
        query.setShopId(shopId);
        return delete(query);
    }

    /**
     * 通过商铺删除商铺轮播图片
     *
     * @param shop 商铺
     * @return 删除图片的数量
     */
    private int deleteByShop(Shop shop) {
        ShopImage query = new ShopImage();
        query.setShopId(shop.getId());
        return mapper.delete(query);
    }
}
